<?php
namespace Home\Controller;
use Common\Controller\HomeController;
class ProductController extends HomeController{

    public function product(){

        if (empty($_GET['p'])) {
            $_GET['p']=1;
        }
        if (!empty($_GET['cateid'])&&$_GET['cateid']>0) {
            $com=M('ComCate')->join('RIGHT JOIN fm_commodity ON fm_commodity.id = fm_com_cate.com_id')->where("cate_id='{$_GET['cateid']}'")->page($_GET['p'].',4')->order('com_addtime')->select();
            $this->count=M('ComCate')->join('RIGHT JOIN fm_commodity ON fm_commodity.id = fm_com_cate.com_id')->where("cate_id='{$_GET['cateid']}'")->order('com_addtime')->count();

            $comimgs=M('ComCate')->join('RIGHT JOIN fm_com_image ON fm_com_image.com_id = fm_com_cate.com_id')->where("cate_id='{$_GET['cateid']}'")->select();
            foreach ($com as $key => $value) {
                foreach ($comimgs as $k => $v) {
                    if ($value['cate_id']==$v['cate_id']&&$value['com_id']==$v['com_id']) {
                        $com[$key]['com_img_name']=$v['com_img_name'];
                    }
                }
            }
        }else{
            //获取商品基本信息，连表了商品图片表
            // $this->commod=M('Commodity')->join(" fm_com_image ON fm_com_image.com_id=fm_commodity.id ")->group('com_id')->where('com_status=1')->order('com_addtime')->page($_GET['p'].',4')->select();
            $com=M('Commodity')->where('com_status=1')->order('com_addtime')->page($_GET['p'].',4')->select();
            foreach ($com as $key => $value) {
                $imgs=M('ComImage')->where("com_id={$value['id']}")->find();
                $com[$key]['com_img_name']=$imgs['com_img_name'];
            }
            $this->count=intval(M('Commodity')->where('com_status=1')->count());//统计商品总数
            

        }
        $this->assign('commod',$com);
        // var_dump($this->count);
        // exit;
        $Page = new \Think\Page($this->count,4);
        $show = $Page->show();
        $this->assign('page',$show);
        $this->assign('productshow',U('Home/Product/product_show'));
        
        $this->display();
    }
    public function product_show(){
        //单个商品详情
        $comid=$_GET['com_id'];
        $this->commod=M('Commodity')->where("com_status=1 AND id='{$comid}'")->select();
        $this->comimg=M('ComImage')->where("com_id={$comid}")->select();
        $this->countimg=M('ComImage')->where("com_id={$comid}")->count();
        $this->store=range(1, $this->commod[0]['com_store']);
        //相关产品
        $topid=M('ComCate')->field('fm_category.id')->join(" fm_category ON fm_category.id=fm_com_cate.cate_id")->where("com_id='{$comid}' AND level=2")->select();

        // print_r($topid);
        foreach ($topid as $key => $value) {
            $commodrand=M('Commodity')->join("fm_com_cate ON fm_com_cate.com_id=fm_commodity.id ")->where("com_status=1 AND cate_id=".$value['id'])->select();
        }
        foreach ($commodrand as $key => $value) {
            $comimgrand=M('ComImage')->where("com_id=".$value['com_id'])->find();
            if ($comimgrand['com_id']==$value['com_id']) {
                $commodrand[$key]['com_img_name']=$comimgrand['com_img_name'];
            }
        }
        shuffle($commodrand);
        $commodrand=array_slice($commodrand,1,5);
        // print_r($commodrand);exit;
        $this->assign('commodrand',$commodrand);
        $this->assign('addShopcar',U('Home/Shopcar/addShopcar'));
        // $this->assign('payply',U("__ROOT__/index.php/Home/Pay/payply.html"));
        $this->display();
    }
}